package com.itcast.donkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.DbAccountModles;

import java.util.Iterator;
import java.util.List;

/**
 * Created by lanouhn on 16/9/18.
 */
public class ConsumNoteDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private ImageView ivEither;
    private TextView tvPercentage1;
    private TextView tvExpenseMoney1;
    private ImageView ivTraffic;
    private TextView tvPercentage2;
    private TextView tvExpenseMoney2;
    private ImageView ivFood;
    private TextView tvPercentage3;
    private TextView tvExpenseMoney3;
    private ImageView ivHotle;
    private TextView tvPercentage4;
    private TextView tvExpenseMoney4;
    private ImageView ivTicket;
    private TextView tvPercentage5;
    private TextView tvExpenseMoney5;
    private ImageView ivShopping;
    private TextView tvPercentage6;
    private TextView tvExpenseMoney6;
    private ImageView ivEntertainment;
    private TextView tvPercentage7;
    private TextView tvExpenseMoney7;
    private ImageView ivTotle;
    private TextView tvExpenseMoney8;

    private List<DbAccountModles> modlesList;
    private double moneyAll;
    private double money1, money2, money3, money4, money5, money6, money7;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes_detail);
        initView();
        initData();
        initEvent();

    }

    private void initView() {
        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        ivEither = (ImageView) findViewById(R.id.iv_either);
        tvPercentage1 = (TextView) findViewById(R.id.tv_percentage1);
        tvExpenseMoney1 = (TextView) findViewById(R.id.tv_expense_money1);
        ivTraffic = (ImageView) findViewById(R.id.iv_traffic);
        tvPercentage2 = (TextView) findViewById(R.id.tv_percentage2);
        tvExpenseMoney2 = (TextView) findViewById(R.id.tv_expense_money2);
        ivFood = (ImageView) findViewById(R.id.iv_food);
        tvPercentage3 = (TextView) findViewById(R.id.tv_percentage3);
        tvExpenseMoney3 = (TextView) findViewById(R.id.tv_expense_money3);
        ivHotle = (ImageView) findViewById(R.id.iv_hotle);
        tvPercentage4 = (TextView) findViewById(R.id.tv_percentage4);
        tvExpenseMoney4 = (TextView) findViewById(R.id.tv_expense_money4);
        ivTicket = (ImageView) findViewById(R.id.iv_ticket);
        tvPercentage5 = (TextView) findViewById(R.id.tv_percentage5);
        tvExpenseMoney5 = (TextView) findViewById(R.id.tv_expense_money5);
        ivShopping = (ImageView) findViewById(R.id.iv_shopping);
        tvPercentage6 = (TextView) findViewById(R.id.tv_percentage6);
        tvExpenseMoney6 = (TextView) findViewById(R.id.tv_expense_money6);
        ivEntertainment = (ImageView) findViewById(R.id.iv_entertainment);
        tvPercentage7 = (TextView) findViewById(R.id.tv_percentage7);
        tvExpenseMoney7 = (TextView) findViewById(R.id.tv_expense_money7);
        ivTotle = (ImageView) findViewById(R.id.iv_totle);
        tvExpenseMoney8 = (TextView) findViewById(R.id.tv_expense_money8);
    }

    private void initData() {

        Intent intent = getIntent();
        String currency_display = intent.getStringExtra("currency_display");
        modlesList = (List<DbAccountModles>) intent.getSerializableExtra("modles");
        for (int i = 0; i < modlesList.size(); i++) {
            DbAccountModles modles = modlesList.get(i);
            int length = modles.getPrice().length() - currency_display.length();
            String resultMoney = modles.getPrice().substring(0, length);
            double a = Double.valueOf(resultMoney);
            moneyAll += a;
        }
        Iterator<DbAccountModles> iterator = modlesList.iterator();
        while (iterator.hasNext()) {
            DbAccountModles modles = iterator.next();
            if ("其他".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money1 += a;
                tvPercentage1.setText(money1 / moneyAll * 100 + "%");
                tvExpenseMoney1.setText(money1 + "RMB");
            }

            if ("交通".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money2 += a;
                tvPercentage2.setText(money2 / moneyAll * 100 + "%");
                tvExpenseMoney2.setText(money2 + "RMB");
            }

            if ("餐饮".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money3 += a;
                tvPercentage3.setText(money3 / moneyAll * 100 + "%");
                tvExpenseMoney3.setText(money3 + "RMB");
            }

            if ("住宿".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money4 += a;
                tvPercentage4.setText(money4 / moneyAll * 100 + "%");
                tvExpenseMoney4.setText(money4 + "RMB");
            }

            if ("门票".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money5 += a;
                tvPercentage5.setText(money5 / moneyAll * 100 + "%");
                tvExpenseMoney5.setText(money5 + "RMB");
            }

            if ("购物".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money6 += a;
                tvPercentage6.setText(money6 / moneyAll * 100 + "%");
                tvExpenseMoney6.setText(money6 + "RMB");
            }

            if ("娱乐".equals(modles.getBrandName())) {
                int length = modles.getPrice().length() - currency_display.length();
                String resultMoney = modles.getPrice().substring(0, length);
                double a = Double.valueOf(resultMoney);
                money7 += a;
                tvPercentage7.setText(money7 / moneyAll * 100 + "%");
                tvExpenseMoney7.setText(money7 + "RMB");
            }
        }
        tvExpenseMoney8.setText(moneyAll + "RMB");
    }

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
        }
    }
}
